@echo off
REM OCH-MRCP 高并发优化启动脚本 (Windows版本)
REM 包含JVM性能调优参数

setlocal enabledelayedexpansion

REM 应用配置
set APP_NAME=och-mrcp
set APP_JAR=target\och-mrcp-0.0.1-SNAPSHOT.jar
set JAVA_OPTS=

REM JVM内存配置（根据服务器配置调整）
set HEAP_SIZE=4g
set HEAP_MIN=2g
set NEW_SIZE=1g
set META_SIZE=256m

REM JVM性能优化参数
set JVM_OPTS=

REM 内存设置
set JVM_OPTS=!JVM_OPTS! -Xms%HEAP_MIN%
set JVM_OPTS=!JVM_OPTS! -Xmx%HEAP_SIZE%
set JVM_OPTS=!JVM_OPTS! -Xmn%NEW_SIZE%
set JVM_OPTS=!JVM_OPTS! -XX:MetaspaceSize=%META_SIZE%
set JVM_OPTS=!JVM_OPTS! -XX:MaxMetaspaceSize=512m

REM G1垃圾收集器配置
set JVM_OPTS=!JVM_OPTS! -XX:+UseG1GC
set JVM_OPTS=!JVM_OPTS! -XX:MaxGCPauseMillis=200
set JVM_OPTS=!JVM_OPTS! -XX:G1HeapRegionSize=16m
set JVM_OPTS=!JVM_OPTS! -XX:G1NewSizePercent=30
set JVM_OPTS=!JVM_OPTS! -XX:G1MaxNewSizePercent=40
set JVM_OPTS=!JVM_OPTS! -XX:G1MixedGCCountTarget=8
set JVM_OPTS=!JVM_OPTS! -XX:InitiatingHeapOccupancyPercent=45

REM JIT编译器优化
set JVM_OPTS=!JVM_OPTS! -XX:+TieredCompilation
set JVM_OPTS=!JVM_OPTS! -XX:TieredStopAtLevel=4
set JVM_OPTS=!JVM_OPTS! -XX:CompileThreshold=10000

REM 字符串优化
set JVM_OPTS=!JVM_OPTS! -XX:+UseStringDeduplication
set JVM_OPTS=!JVM_OPTS! -XX:+OptimizeStringConcat

REM 锁优化
set JVM_OPTS=!JVM_OPTS! -XX:+UseBiasedLocking
set JVM_OPTS=!JVM_OPTS! -XX:+UseThreadPriorities
set JVM_OPTS=!JVM_OPTS! -XX:+DoEscapeAnalysis

REM 网络和I/O优化
set JVM_OPTS=!JVM_OPTS! -Djava.net.preferIPv4Stack=true
set JVM_OPTS=!JVM_OPTS! -Dio.netty.recycler.maxCapacityPerThread=0
set JVM_OPTS=!JVM_OPTS! -Dio.netty.leakDetection.level=simple

REM 安全随机数优化
set JVM_OPTS=!JVM_OPTS! -Djava.security.egd=file:/dev/./urandom

REM 创建日志目录
if not exist logs\gc mkdir logs\gc
if not exist logs\heapdump mkdir logs\heapdump

REM GC日志配置
set JVM_OPTS=!JVM_OPTS! -XX:+PrintGC
set JVM_OPTS=!JVM_OPTS! -XX:+PrintGCDetails
set JVM_OPTS=!JVM_OPTS! -XX:+PrintGCTimeStamps
set JVM_OPTS=!JVM_OPTS! -XX:+PrintGCApplicationStoppedTime
set JVM_OPTS=!JVM_OPTS! -Xloggc:logs\gc\gc.log
set JVM_OPTS=!JVM_OPTS! -XX:+UseGCLogFileRotation
set JVM_OPTS=!JVM_OPTS! -XX:NumberOfGCLogFiles=5
set JVM_OPTS=!JVM_OPTS! -XX:GCLogFileSize=100M

REM OOM处理
set JVM_OPTS=!JVM_OPTS! -XX:+HeapDumpOnOutOfMemoryError
set JVM_OPTS=!JVM_OPTS! -XX:HeapDumpPath=logs\heapdump\

REM 应用参数
set APP_OPTS=
set APP_OPTS=!APP_OPTS! --spring.profiles.active=prod
set APP_OPTS=!APP_OPTS! --server.port=8080
set APP_OPTS=!APP_OPTS! --logging.file.path=logs\

echo ========================================
echo OCH-MRCP High Concurrency Startup
echo ========================================

REM 检查Java版本
java -version
if errorlevel 1 (
    echo Error: Java not found. Please install Java 17 or higher.
    pause
    exit /b 1
)

REM 检查JAR文件
if not exist "%APP_JAR%" (
    echo Error: JAR file not found: %APP_JAR%
    echo Please run 'mvn clean package' first
    pause
    exit /b 1
)

echo.
echo System Information:
echo   Java Options: !JVM_OPTS!
echo   App Options: !APP_OPTS!
echo.

REM 创建日志目录
if not exist logs mkdir logs

echo Starting %APP_NAME% with optimized JVM settings...

REM 启动应用
start "OCH-MRCP" java !JVM_OPTS! -jar %APP_JAR% !APP_OPTS!

echo.
echo Application started successfully!
echo Console output will be displayed in the new window.
echo.
echo Health check: http://localhost:8080/actuator/health
echo Metrics: http://localhost:8080/actuator/metrics
echo.
echo Press any key to exit this script...
pause > nul
